package com.eostoken.eos_wallet.ui.activity;

import android.content.Intent;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.BarUtils;
import com.blankj.utilcode.util.BusUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.MapUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eostoken.eos_wallet.R;
import com.eostoken.eos_wallet.base.BaseActivity;
import com.eostoken.eos_wallet.bus.ToWallet;
import com.eostoken.eos_wallet.db.MgpWallet;
import com.eostoken.eos_wallet.mvp.callback.CommonCallback;
import com.eostoken.eos_wallet.utils.WalletUtils;
import com.fredy.mvp.BasePresenter;
import com.fredy.mvp.BaseView;
import com.fredy.mvp.Model;
import com.qmuiteam.qmui.util.QMUIDisplayHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView;
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

import static com.eostoken.eos_wallet.utils.Constants.BUS_TO_WALLET;
import static com.eostoken.eos_wallet.utils.Constants.CUT_WALLET;

public class WalletListActivity extends BaseActivity {


    @BindView(R.id.topbar)
    QMUITopBarLayout topbar;
    @BindView(R.id.groupListView)
    QMUIGroupListView groupListView;

    private List<MgpWallet> allWallets;
    private Map<String, MgpWallet> hashMap;
    private MgpWallet mCurrentMgpWallet;

    @Override
    public int getContentView() {
        return R.layout.fragment_dashboard;
    }

    @Override
    protected void initData() {
        hashMap = MapUtils.newLinkedHashMap();
        mCurrentMgpWallet = WalletUtils.getCurrentWallet(this);
        WalletUtils.getAllWalletList(this, new CommonCallback() {
            @Override
            public void onSuccess(Object data, String successMsg, int responseSuccessCode) {
                allWallets = (List<MgpWallet>) data;
                Collections.reverse(allWallets);
                hashMap.clear();
                hashMap.put(mCurrentMgpWallet.getCurrentAccountName(), mCurrentMgpWallet);
                if (allWallets != null) {
                    if (allWallets.size() > 0) {
                        for (MgpWallet mgpWallet : allWallets) {
                            if (!ObjectUtils.equals(mgpWallet.getCurrentAccountName(), mCurrentMgpWallet.getCurrentAccountName()))
                                hashMap.put(mgpWallet.getCurrentAccountName(), mgpWallet);
                        }
                    }
                }
                initGroupListView();
            }

            @Override
            public void onFailure(String msg, int responseFailCode) {

            }
        });

    }

    @Override
    public void initView() {
        BarUtils.setStatusBarColor(this, ContextCompat.getColor(this, R.color.app_color_blue));
        BarUtils.setStatusBarLightMode(this, false);
        topbar.setTitle(StringUtils.getString(R.string.str_accounts_list));
        topbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        topbar.addRightImageButton(R.mipmap.add_wallet_icon, R.id.topbar_right_change_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(WalletListActivity.this, AddWalletActivity.class);
                intent.putExtra("isMain", false);
                ActivityUtils.startActivity(intent);
            }
        });
    }

    private void initGroupListView() {
        View.OnClickListener onClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (v instanceof QMUICommonListItemView) {
                    CharSequence text = ((QMUICommonListItemView) v).getText();
                    MgpWallet wallet = hashMap.get(text);
                    wallet.setCurrentAccountName(text.toString());
                    WalletUtils.updateWallet(WalletListActivity.this, wallet);
                    WalletUtils.setCurrentAccountName(text.toString());
                    WalletUtils.setCurrentPrivatekey(wallet.getPrivateKey());
//                    Intent intent = new Intent();
//                    intent.putExtra(Constants.INTENT_EXTRA_KEY_WALLET, wallet);
//                    getActivity().setResult(RESULT_OK, intent);
                    BusUtils.post(BUS_TO_WALLET, new ToWallet(wallet, CUT_WALLET));
                    finish();
                }
            }
        };

        QMUIGroupListView.Section section = QMUIGroupListView.newSection(WalletListActivity.this);
        for (Map.Entry<String, MgpWallet> entry : hashMap.entrySet()) {
//            System.out.println("Key: "+ entry.getKey()+ " Value: "+entry.getValue());
            QMUICommonListItemView itemView = groupListView.createItemView(entry.getKey());
            itemView.setAccessoryType(QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON);
            itemView.setOrientation(QMUICommonListItemView.VERTICAL);
            if (ObjectUtils.equals(entry.getKey(), mCurrentMgpWallet.getCurrentAccountName())) {
                itemView.setTipPosition(QMUICommonListItemView.TIP_POSITION_RIGHT);
                itemView.showRedDot(true);
            }
            section.addItemView(itemView, onClickListener);
        }
        section.setMiddleSeparatorInset(QMUIDisplayHelper.dp2px(WalletListActivity.this, 16), 0);
        section.addTo(groupListView);
        groupListView.removeViewAt(0);

    }

    @Override
    public Model createModel() {
        return null;
    }

    @Override
    public BaseView createView() {
        return null;
    }

    @Override
    public BasePresenter createPresenter() {
        return null;
    }

    @Override
    public void finishRefresh() {

    }

    @Override
    public boolean isShowState() {
        return true;
    }
}
